//
//  GXSearchResourceViewController.m
//  GXLanOuADemo
//
//  Created by 甘霞 on 15/11/3.
//  Copyright © 2015年 甘霞. All rights reserved.
//

#import "GXSearchResourceViewController.h"
#import "GXiaNetHandle.h"
#import "WebSiteManager.h"
#import "GXSuggestModel.h"
#import "GXSpecialModel.h"
#import "GXSuggestArticelTableViewCell.h"
#import "GXSuggestImageViewTableViewCell.h"
#import "NSString+GXSizeWithText.h"
#import "GXProviderDetailTableViewCell.h"
#import "MBProgressHUD.h"
#import "UIScrollView+EmptyDataSet.h"
#import "GXSearchResultDetailViewController.h"

static NSString *articleCellID = @"SuggestArticleCell";
static NSString *poetryCellID = @"SuggestPoetryCell";
static NSString *articleNoCellID = @"SuggestArticleNoCell";
@interface GXSearchResourceViewController () < UITableViewDataSource, UITableViewDelegate, DZNEmptyDataSetDelegate, DZNEmptyDataSetSource>

//推荐数据
@property (nonatomic, retain) NSMutableArray *itemArr;
//专题数据
@property (nonatomic, retain) NSMutableArray *speacialArr;
//全部数据
@property (nonatomic, retain) NSMutableArray *allDataArr;

@end

@implementation GXSearchResourceViewController

#pragma mark - 视图将要出现
- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    self.navigationController.navigationBarHidden = NO;
    NSArray *temp = self.tabBarController.view.subviews;
    UIView *tabbarView = temp[2];
    [UIView animateWithDuration:1 animations:^{
        tabbarView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 49);
    }];
}

#pragma mark - 视图加载成功
- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self.view setBackgroundColor:[UIColor whiteColor]];
    
    //初始化数组
    self.itemArr = [NSMutableArray array];
    self.speacialArr = [NSMutableArray array];
    self.allDataArr = [NSMutableArray array];
    self.navigationItem.title = @"为您找到";
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"返回" style: UIBarButtonItemStylePlain target:self action:@selector(backAction)];

    [self.navigationController.navigationBar setTintColor:[UIColor blackColor]];
    //创建 tableView
    [self createSuggestTableView];
    //请求
    [self getSearchResultData];
    // 加载小菊花
    [self loadNightMBProgress];

}

#pragma mark - 回退按钮响应方法
- (void)backAction {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 导航栏右按钮响应事件
- (void)buttonClick:(UIBarButtonItem *)btn {
    [self.navigationController popToRootViewControllerAnimated:YES];
}

#pragma mark - 视图将要消失
- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    self.navigationController.navigationBarHidden = NO;
}

#pragma mark - 请求搜索结果数据
- (void)getSearchResultData {
    //请求数据
    NSString *url = [NSString stringWithFormat:kSearchResultWeb, self.str];
    [GXiaNetHandle getUrl:url cookie:nil success:^(id result) {
        //隐藏小菊花
        self.hud.hidden = YES;
        //将请求到的推荐搜索添加到数组中
        //推荐数据
        self.itemArr = [GXSuggestModel transformWithArray:result[@"item"]];
        //专题数据
        self.speacialArr = [GXSpecialModel transformWithArray:result[@"special"]];
        //将所有的数组放到数据数组
        [self.allDataArr addObjectsFromArray:self.itemArr];
        [self.allDataArr addObjectsFromArray:self.speacialArr];
        //刷新视图
        if (self.allDataArr.count) {
            [self.tableView reloadData];
            [self.hud removeFromSuperview];
        }
    } failure:^(NSError *error) {
        NSLog(@"error:%@", error);
    }];
}


#pragma mark - 创建 tableView
- (void)createSuggestTableView {
    //创建 tableView
    [self createTableViewWithFrame:CGRectMake(0, 64, kScreenWidth, kScreenHeight - 64)];
    //设置代理
    self.tableView.delegate = self;
    //设置数据源
    self.tableView.dataSource = self;
    //空数据代理
    self.tableView.emptyDataSetSource = self;
    self.tableView.emptyDataSetDelegate = self;
    //取消滚动条
    self.tableView.showsVerticalScrollIndicator = NO;
    //注册文章 cell
    [self.tableView registerClass:[GXSuggestArticelTableViewCell class] forCellReuseIdentifier:articleCellID];
    //注册诗歌 cell
    [self.tableView registerClass:[GXSuggestImageViewTableViewCell class] forCellReuseIdentifier:poetryCellID];
    //不推荐文章
    [self.tableView registerClass:[GXProviderDetailTableViewCell class] forCellReuseIdentifier:articleNoCellID];
}

#pragma mark - 空数据
- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView {
    self.tableView.separatorStyle = UITableViewCellSelectionStyleNone;
    return [UIImage imageNamed:@"special-has-contents-updated"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView {
    NSString *text = @"搜索不到数据...%>_<%";
    NSDictionary *str = @{NSFontAttributeName : [UIFont boldSystemFontOfSize:16.0f]};
    return [[NSAttributedString alloc] initWithString:text attributes:str];
}

#pragma mark - tableView 代理/数据源
//共有几行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    //返回瀑布流数据的个数
    return self.allDataArr.count;
}

//每行显示什么内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //重用文章 cell
    GXSuggestArticelTableViewCell *articleCell = [tableView dequeueReusableCellWithIdentifier:articleCellID];
    //重用诗歌 cell
    GXSuggestImageViewTableViewCell *poetryCell = [tableView dequeueReusableCellWithIdentifier:poetryCellID];
    //不推荐文章 cell
    GXProviderDetailTableViewCell *providerCell = [tableView dequeueReusableCellWithIdentifier:articleNoCellID];
    //取得模型
    id temp = self.allDataArr[indexPath.row];
    if ([temp isKindOfClass:[GXSuggestModel class]]) {
        //取得推荐模型
        GXSuggestModel *suggestModel = (GXSuggestModel *)temp;
        //九宫格 大型画册 视频
        if (suggestModel.type.integerValue == 1 || suggestModel.type.integerValue == 2 || suggestModel.type.integerValue == 9 || suggestModel.type.integerValue == 10) {
            articleCell.suggestModel = suggestModel;
            return articleCell;
        }
        //文章
        else if (suggestModel.type.integerValue == 0) {
            //推荐文章
            if (suggestModel.is_recommended == true) {
                articleCell.suggestModel = suggestModel;
                return articleCell;
            }
            //不推荐文章
            else {
                providerCell.suggestModel = suggestModel;
                return providerCell;
            }
        }
        //大图 cell
        else if (suggestModel.type.integerValue == 5 || suggestModel.type.integerValue == 6){
            poetryCell.suggestModel = suggestModel;
            return poetryCell;
        }
        return poetryCell;
    }
    return poetryCell;
}

//tableView 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    GXSuggestModel *suggestModel = self.allDataArr[indexPath.row];
    id temp = self.allDataArr[indexPath.row];
    
    if ([temp isKindOfClass:[GXSuggestModel class]]) {
        //文本宽度
        CGFloat titleW = kScreenWidth - 25 - [UIImage imageNamed:@"icon-image-album"].size.width;
        //标题文本高度
        CGRect titleSize = [suggestModel.title SizeWithTextFontSize:kSuggestTitleFontSize textWidth:titleW];
        //副标题文本高度
        CGRect subTitleSize = [suggestModel.content[@"desc"] SizeWithTextFontSize:KSuggestIntroFontSize textWidth:titleW];
        
        //音乐/大型画册/九宫格/视频
        if (suggestModel.type.integerValue == 10 || suggestModel.type.integerValue == 9 || suggestModel.type.integerValue == 1 || suggestModel.type.integerValue == 2) {
            //返回行高
            return 330 + titleSize.size.height + subTitleSize.size.height + 0.1;
        }
        //文章
        else if (suggestModel.type.integerValue == 0) {
            //推荐文章与不推荐文章返回高度
            return suggestModel.is_recommended == false ? 150 : 330 + titleSize.size.height + subTitleSize.size.height + 0.1;
        }
        //诗歌
        else if (suggestModel.type.integerValue == 5 || suggestModel.type.integerValue == 6){
            return 350;
        } else {
            return 0;
        }
    }
    return 0;
}

#pragma mark - 搜索结果点击方法
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    //取消选中
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //搜索结果控制
    GXSearchResultDetailViewController *resultDetailVC = [[GXSearchResultDetailViewController alloc] init];
    //属性传值
    resultDetailVC.suggestModel = self.allDataArr[indexPath.row];
    //push
    [self.navigationController pushViewController:resultDetailVC animated:YES];
}

@end
